package com.hongdee.scm.interior.controller;

import com.alibaba.druid.util.StringUtils;
import com.hongdee.base.entity.ResultEntity;
import com.hongdee.scm.interior.entity.TScmInterior;
import com.hongdee.scm.interior.service.ITScmInteriorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 名  称:TScmInteriorController
 * 类  型:class
 * 描  述:
 * 路  径:com.hongdee.scm.interior.controller
 * 作  者: 术洪超
 * 日  期:2017-05-14 11:03
 */

@RestController
@RequestMapping("/scm/interior")
public class TScmInteriorController {

    @Autowired
    private ITScmInteriorService itScmInteriorService;

    /**
     * @param uid           登录用户 ID
     * @param token         登录用户 Token
     * @param start         起始记录
     * @param limit         页面大小
     * @param interiorCode  内饰代码
     * @param interiorName  内饰名称
     * @param carSeriesId 车系id
     * @return Map
     * @throws Exception
     * @desc 通过 JDBCDao分页查询
     * @author 术洪超
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestParam(name = "interiorCode", defaultValue = "") String interiorCode,
                    @RequestParam(name = "interiorName", defaultValue = "") String interiorName,
                    @RequestParam(name = "carSeriesId", defaultValue = "") String carSeriesId) throws Exception {

        Map map = itScmInteriorService.page(start, limit, interiorCode, interiorName, carSeriesId, uid);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * @param uid   登录用户 ID
     * @param token 登录用户 Token
     * @param id    主键 ID
     * @return ResultEntity
     * @throws Exception
     * @desc 查询详情
     * @author 术洪超
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择颜色！");
        }
        Map map = itScmInteriorService.info(id, uid);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }

    /**
     * @param data 要保存的实体对象
     * @return Map
     * @throws Exception
     * @desc 保存添加或者修改的实体内容，通过主键判断
     * @author 术洪超
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TScmInterior data) throws Exception {
        TScmInterior interior = this.itScmInteriorService.save(data, uid);
        ResultEntity res = new ResultEntity();
        res.setMsg("保存成功！");
        res.setSuccess(true);
        res.setData(interior);
        return res;
    }

    /**
     * @param carSeriesId 车系ID
     * @return List
     * @throws Exception
     * @desc 内饰下拉菜单
     * @author 术洪超
     */
    @RequestMapping("/combo")
    @ResponseBody
    public List<Map> combo(@RequestParam(name = "uid", defaultValue = "") String uid,
                           @RequestParam(name = "seriesId", defaultValue = "") String carSeriesId
    ) throws Exception {
        return this.itScmInteriorService.combo(uid, carSeriesId);
    }


    /**
     * 车系内饰树
     */
    @RequestMapping("/tree")
    @ResponseBody
    public List tree(@RequestParam(name = "uid", defaultValue = "") String uid,
                     @RequestParam(name = "token", defaultValue = "") String token,
                     @RequestParam(name = "allSeries", defaultValue = "") String allSeries
                     //@RequestParam(name = "showLevel", defaultValue = "2") int showLevel
    ) throws Exception {

        List treeList = this.itScmInteriorService.buildTree(allSeries);
        ResultEntity res = new ResultEntity();
        res.setDataList(treeList);
        res.setSuccess(true);
        res.setMsg("查询车系内饰成功");
        return treeList;
    }

    /**
     * 是否停用内饰
     * @param uid
     * @param token
     * @param interiorIds
     * @return
     * @throws Exception
     */
    @RequestMapping("/interiorCancel")
    @ResponseBody
    public ResultEntity interiorCancel(@RequestParam(name = "uid", defaultValue = "") String uid,
                                        @RequestParam(name = "token", defaultValue = "") String token,
                                        @RequestBody String[] interiorIds ) throws Exception {

        this.itScmInteriorService.interiorCancel(interiorIds,uid);
        ResultEntity res = new ResultEntity();
        res.setMsg("已停用！");
        res.setSuccess(true);
        return res;
    }
}
